


#include <iostream>

#include <boost/tr1/memory.hpp>
#include <boost/thread/thread.hpp>


#include "../classes_net/DownloadParser.h"
#include "../classes_net/URL.h"
#include "../classes_dados/ManagerBerthier.h"
#include "../classes_outros/Config.h"
#include "../classes_outros/RobotsDownloadInterface.h"

#include "ThreadFunctionDatabase.h"

int ThreadFunctionDatabase::numPaginas = 0;
int ThreadFunctionDatabase::numThreadsRobots = 0;
int ThreadFunctionDatabase::maxNumThreadsRobots = 0;


boost::mutex ThreadFunctionDatabase::numPaginasMutex;


void * ThreadFunctionDatabase::manageDownloaderQueues(void * t) {
	std::tr1::shared_ptr<ManagerBerthier> mb = ManagerBerthier::getInstance();
	bool conseguiu;
	boost::xtime dorme;
	
	while (true)
	{
		conseguiu = mb->refillQueuesOnce();
		if (!conseguiu){
			cout << "ZzzZzzZzz" << endl;
			boost::xtime_get(&dorme, boost::TIME_UTC);
			dorme.sec += Config::getQueueThreadSleepTime();
			boost::thread::sleep(dorme);
//			boost::thread::yield();
//			boost::this_thread::yield();
		}
	}
}

void * ThreadFunctionDatabase::getDomainRobots(void *domParam) {

//	if(numThreadsRobots>maxNumThreadsRobots) maxNumThreadsRobots = numThreadsRobots;
//	cout << "ThreadFunctionDatabase::getDomainRobots: -------------------> numThreadsRobots    = " << numThreadsRobots << endl;
//	cout << "ThreadFunctionDatabase::getDomainRobots: -------------------> maxNumThreadsRobots = " << maxNumThreadsRobots << endl;
//	Dominio * dominio = (Dominio *)(domParam);
//	DownloadParser downloader;
//	//	cout << "Puxando o robots.txt do dominio " << domain << endl;
//	downloader.setURL(dominio->getName());
//	Robots * robots =  new Robots( downloader.getRobot() );
//	dominio->setRobots(robots);
//	numThreadsRobots--;
	while (true) {
		Dominio * dominio = RobotsDownloadInterface::get();
		DownloadParser downloader;
		downloader.setURL(dominio->getName());
		Robots * robots =  new Robots( downloader.getRobot() );
		dominio->setRobots(robots);
	}
}


void * ThreadFunctionDatabase::downloadInit(void * t){
	int id = int(t);
	
	try{
	int dormeTimeOut = 0;
		
	std::tr1::shared_ptr<URLFileDealer> urlFileDealer = URLFileDealer::getInstance();
	std::tr1::shared_ptr<ManagerBerthier> mb = ManagerBerthier::getInstance();
	DownloadParser downloadTeste;
	
	URL * tempUrl;
	list<URL> urlList;
	list<URL>::iterator it;
	
	tempUrl = mb->getNewURL(id);
	
	while(dormeTimeOut < Config::getDownloaderThreadNumSleeps()){//3){
		//cout << "dormeTimeOut < 3 " << endl;
		while( tempUrl != NULL ){
			dormeTimeOut = 0;
			
//			if (tempUrl->get_dominio().size() < 5) {
//				cout << "\n\n\n\n\n\n\n\n\n\n\n\n" << endl;
//				cout << "-----------------------------------------------------------------------------------------------------------" << endl;
//				cout << "-----------------------------------------------------------------------------------------------------------";
//				cout << tempUrl->toString() << endl;		
//			}
			
			
			
			cout << "Copiando: " << tempUrl->toString() << endl;
			
			{
				boost::mutex::scoped_lock numPaginasLock(numPaginasMutex);
				numPaginas++;
				if ((numPaginas % 100)== 0){
					cout << "\n\n\n\n\n\n\n\n\n\n\n\n" << endl;
					cout << "-----------------------------------------------------------------------------------------------------------" << endl;
					cout << "-----------------------------------------------------------------------------------------------------------";
					cout << "numPaginas: " << numPaginas << endl;
					cout << "-----------------------------------------------------------------------------------------------------------";
					cout << "numPaginas: " << numPaginas << endl;
					cout << "-----------------------------------------------------------------------------------------------------------";
					cout << "numPaginas: " << numPaginas << endl;
					cout << "-----------------------------------------------------------------------------------------------------------";
					cout << "numPaginas: " << numPaginas << endl;
					cout << "-----------------------------------------------------------------------------------------------------------";
					cout << "numPaginas: " << numPaginas << endl;
					cout << "-----------------------------------------------------------------------------------------------------------";
					cout << "numPaginas: " << numPaginas << endl;
					cout << "-----------------------------------------------------------------------------------------------------------";
					cout << "numPaginas: " << numPaginas << endl;
					cout << "-----------------------------------------------------------------------------------------------------------";
					cout << "numPaginas: " << numPaginas << endl;
					cout << "-----------------------------------------------------------------------------------------------------------";
					cout << "numPaginas: " << numPaginas << endl;
					cout << "-----------------------------------------------------------------------------------------------------------";
					cout << "numPaginas: " << numPaginas << endl;
					cout << "-----------------------------------------------------------------------------------------------------------";
					cout << "numPaginas: " << numPaginas << endl;
					cout << "-----------------------------------------------------------------------------------------------------------" << endl;
				}
				//cout << numPaginas << "\t- " << tempUrl->toString() << endl;
				}
			downloadTeste.setURL(tempUrl->toString());
			urlList = downloadTeste.getNewURLs();
			
			cout << "TAMANHO DA LISTA OBTIDA:  " << urlList.size() << "   -   ";
			urlFileDealer->addURLs(urlList);
			cout << "ADICIONADO!" << endl;
			//for(it = urlList.begin(); it != urlList.end(); it++) 	urlFileDealer->addToDiskBuffer(*it);
			delete tempUrl;
			tempUrl = mb->getNewURL(id);
		}
		
		cout << "Sem novas urls disponiveis..." << endl;
		// dorme 
		
		boost::xtime dorme;
		boost::xtime_get(&dorme, boost::TIME_UTC);
		dorme.sec += Config::getDownloaderThreadSleepTime();
		boost::thread::sleep(dorme);
		dormeTimeOut ++;
		
		tempUrl = mb->getNewURL(id);
	}
	
	}catch(...){
		
		cout << "MC causou problema em algo...Ver ThreadFunctionDatabase" << endl;
	}
	
	//return "MC Suga"; // return generico
}



